package com.youzi.usercenter.service;

import com.youzi.corazon.common.exception.ServiceException;
import com.youzi.corazon.common.security.Md5Util;
import com.youzi.usercenter.form.LoginForm;
import com.youzi.usercenter.model.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by tanxl on 2017-12-18.
 */
@Service
@Transactional
public class LoginService {

    @Autowired
    UserService userService;

    public TUser login(LoginForm form) {
        TUser user = userService.findByUsername(form.getUsername());
        if (user == null) {
            throw new ServiceException("用户名或密码错误");
        }

        String password = user.getPassword();
        String encryptedPassword = Md5Util.md5(form.getPassword());
        if (!password.equals(encryptedPassword)) {
            throw new ServiceException("用户名或密码错误");
        }

        Integer status = user.getStatus();
        if (status != 1) {
            throw new ServiceException("用户已锁定，请联系管理员");
        }
        return user;
    }


}
